import { defineStore } from "pinia";
import {createVNode, render, VNode} from "vue";
export default defineStore({
    id:'window',
    state: (): {[key:string]:any} => ({
        minSizeList: new Set() , // 存储最小化的windowId
        openWindowList: new Set(), // 已经打开的窗口
        zIndex:1, // 窗口的层级
    }),
    actions:{
        /** 设置全部窗口层级 */
        setZIndex(index:number){
            this.zIndex = index
        },
        /** 设置最小化列表 */
        addMinSizeList(windowInfo:any){
            this.minSizeList.add(windowInfo)
        },
        /** 移除最小化列表 */
        removeMinSizeById(windowInfo:any){
            this.minSizeList.delete(windowInfo)
        },
        /** 添加打开的窗口 */
        addOpenWindowList(windowInfo:any){
            this.openWindowList.add(windowInfo)
        },
        /** 移除打开的窗口 */
        removeOpenWindowById(windowId:string){
            this.openWindowList.delete(windowId)
        },
        /** 设置某个应用层级 */
        setApplicationZIndex(applicationId:string, zIndex?:number){
            console.log(applicationId)
            const application = Array.from(this.$state.minSizeList).find(( ele:any) => ele.id === applicationId )
            if(application){
                //@ts-ignore
                application.zIndex = (zIndex ?? this.$state.zIndex) + 1
            }
        },
        /** 打开应用窗口 */
        openWindow(name:string,vNode?:VNode){
            const applicationId = Array.from(this.$state.openWindowList).find((ele:any) => ele.indexOf(name) !== -1)
            if(applicationId) return this.setApplicationZIndex(<string>applicationId)
            if(!vNode) return console.error('未传入VNode应用程序实例')
            const VNode = createVNode(vNode)
            const content: any = document.createElement('div')
            render(VNode, content)
            document.body.appendChild(content.firstElementChild)
        }
    },
})
